package com.example.finalwork.Pay;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.finalwork.DAO.PayDAO;
import com.example.finalwork.Entity.Pay;
import com.example.finalwork.LoginActivity;
import com.example.finalwork.R;
import com.example.finalwork.lmpl.PayDAOlmpl;

import java.util.List;

import static org.junit.Assert.assertNotEquals;

public class PayAddActivity extends AppCompatActivity implements View.OnClickListener{
    private static final String TAG = "PayAddActivity";
    private EditText pay_nametext,pay_datetext,pay_amounttext,pay_remarktext;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_payadd);

        findViewById(R.id.pay_add_button).setOnClickListener(this);
        findViewById(R.id.pay_exit1).setOnClickListener(this);

        pay_nametext = findViewById(R.id.pay_nametext);
        pay_datetext = findViewById(R.id.pay_datetext);
        pay_amounttext = findViewById(R.id.pay_amounttext);
        pay_remarktext = findViewById(R.id.pay_remarktext);

        setTitle("增加支出项目功能");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.pay_add_button:
                if(pay_nametext.getText().toString().equals(""))
                {
                    Toast.makeText(this,"支出详细不能为空",Toast.LENGTH_SHORT).show();
                    pay_nametext.requestFocus();
                    break;
                }
                else if(pay_datetext.getText().toString().equals(""))
                {
                    Toast.makeText(this,"支出日期不能为空",Toast.LENGTH_SHORT).show();
                    pay_datetext.requestFocus();
                    break;
                }
                else if(pay_amounttext.getText().toString().equals(""))
                {
                    Toast.makeText(this,"支出金额不能为空",Toast.LENGTH_SHORT).show();
                    pay_amounttext.requestFocus();
                    break;
                }
                if(pay_remarktext.getText().toString().equals(""))
                {
                    pay_remarktext.setText("无");
                }
                PayDAO payDAO = new PayDAOlmpl(this);
                List<Pay> pays = payDAO.GetAllPay();
                Pay pay = new Pay();
                pay.setPay_Id(pays.size() + 1);
                pay.setPay_User(LoginActivity.data);
                pay.setPay_Name(pay_nametext.getText().toString());
                pay.setPay_Date(pay_datetext.getText().toString());
                pay.setPay_Amount(pay_amounttext.getText().toString());
                pay.setPay_Remark(pay_remarktext.getText().toString());
                long result = payDAO.addPay(pay);
                Log.d(TAG,"add Pay result - >" + result);
                assertNotEquals(-1,result);
                Toast.makeText(this,"添加成功，返回上一级",Toast.LENGTH_SHORT).show();
                startActivity(new Intent(this, PayMainActivity.class));
                break;
            case R.id.pay_exit1:
                Toast.makeText(this,"返回上一级",Toast.LENGTH_SHORT).show();
                startActivity(new Intent(this, PayMainActivity.class));
                break;
        }
    }
}
